/**
 * 专门处理运价查询的Ajax.
 */
var provice;
var city;
var company;

//初始化运货选择工具。
function initialPriceTools(proviceElementId, cityElementId, companyElementId) {
	provice = proviceElementId;
	city = cityElementId;
	company = companyElementId;
	
	ajaxQueryProvinces(function(){
		//清空下拉菜单
		emptySelector($("#"+city));
		emptySelector($("#"+company));
		//增加改变事件
		$("#"+provice).change(proviceChanged);
		$("#"+city).change(cityChanged);
	});
}

//有值的情况下初始化运货选择工具。
function initialPriceToolsWithValues(proviceElementId, cityElementId, companyElementId, valueId, isreadonly) {
	if(!valueId && valueId=='') {
		initialPriceTools(proviceElementId, cityElementId, companyElementId);
		return;
	}
	provice = proviceElementId;
	city = cityElementId;
	company = companyElementId;
	
	$.ajax({
		url: "/transprice/"+valueId,
		type: "GET",
		dataType: "json",
		complete: function(XMLHttpRequest, textStatus, errorThrown) {
			if(textStatus == "error") {
				var errJson = eval("("+XMLHttpRequest.responseText+")");
				var errMsg = "省份查询失败，"+errJson.message;
				$("#errMsg").html(errMsg);
				alert(errMsg);
			} else {
				var values = eval("("+XMLHttpRequest.responseText+")");

				$("#"+provice).change(proviceChanged);
				$("#"+city).change(cityChanged);
				ajaxQueryProvinces(function(){
					ajaxQueryCities(function(){
						ajaxQueryTransPrices(null, values.transferPriceId, isreadonly);
					}, values.cityId, isreadonly);
				}, 
				values.provinceId, isreadonly);
			}
		}
	});
}

/**
 * 更改省份后的动作。
 */
function proviceChanged() {
	var proviceId = $("#"+provice).val();
	
	if(!proviceId || proviceId == "") {
		emptySelector($("#"+company));
		emptySelector($("#"+city));
		return;
	}
	ajaxQueryCities(function(){
		//清空公司下拉菜单。
		emptySelector($("#"+company));
	});
}

/**
 * 更改城市后的动作。
 */
function cityChanged() {
	var destCityId = $("#"+city).val();
	if(!destCityId || destCityId == "") {
		emptySelector($("#"+company));
		return;
	}
	ajaxQueryTransPrices();
}

function emptySelector(selector, isreadonly) {
	$(selector).html("");
	if(!isreadonly) $(selector).append("<option value=''>--请选择--</option>");
}

function ajaxQueryProvinces(callback, value, isreadonly) {
	$.ajax({
		url: "/transprice/provinces",
		//data: dataStr,
		type: "GET",
		dataType: "json",
		complete: function(XMLHttpRequest, textStatus, errorThrown) {
			if(textStatus == "error") {
				var errJson = eval("("+XMLHttpRequest.responseText+")");
				var errMsg = "省份查询失败，"+errJson.message;
				$("#errMsg").html(errMsg);
				alert(errMsg);
			} else {
				var provices = eval("("+XMLHttpRequest.responseText+")");
				var proviceElement = $("#"+provice);
				emptySelector(proviceElement, isreadonly);
				var optStr = "";
				for(var i=0 ; i<provices.length ; i++) {
					if(value == provices[i].id) {
						optStr = optStr + "<option selected value=" + provices[i].id + ">" + provices[i].name + "</option>";
					} else if(!isreadonly) {
						optStr = optStr + "<option value=" + provices[i].id + ">" + provices[i].name + "</option>";
					}
				}
				$(proviceElement).append(optStr);
				if(callback) callback();
			}
		}
	});
}

function ajaxQueryCities(callback, value, isreadonly) {
	var proviceId = $("#"+provice).val();
	$.ajax({
		url: "/transprice/cities",
		data: "provinceId=" + proviceId,
		type: "POST",
		dataType: "json",
		complete: function(XMLHttpRequest, textStatus, errorThrown) {
			if(textStatus == "error") {
				var errJson = eval("("+XMLHttpRequest.responseText+")");
				var errMsg = "城市查询失败，"+errJson.message;
				$("#errMsg").html(errMsg);
				alert(errMsg);
			} else {
				var cities = eval("("+XMLHttpRequest.responseText+")");
				var cityElement = $("#"+city);
				emptySelector(cityElement, isreadonly);
				var optStr = "";
				for(var i=0 ; i<cities.length ; i++) {
					if(value == cities[i].id) {
						optStr = optStr + "<option selected value=" + cities[i].id + ">" + cities[i].cityName + "</option>";
					} else if(!isreadonly) {
						optStr = optStr + "<option value=" + cities[i].id + ">" + cities[i].cityName + "</option>";
					}
				}
				$(cityElement).append(optStr);
				if(callback) callback();
			}
		}
	});
}

function ajaxQueryTransPrices(callback, value, isreadonly) {
	var oriCityId = 1;
	var destCityId = $("#"+city).val();
	$.ajax({
		url: "/transprice/transPrices",
		data: "oriCityId=" + oriCityId + "&destCityId=" + destCityId,
		type: "POST",
		dataType: "json",
		complete: function(XMLHttpRequest, textStatus, errorThrown) {
			if(textStatus == "error") {
				var errJson = eval("("+XMLHttpRequest.responseText+")");
				var errMsg = "城市查询失败，"+errJson.message;
				$("#errMsg").html(errMsg);
				alert(errMsg);
			} else {
				var transPrices = eval("("+XMLHttpRequest.responseText+")");
				var companyElement = $("#"+company);
				emptySelector(companyElement, isreadonly);
				var optStr = "";
				for(var i=0 ; i<transPrices.length ; i++) {
					if(value == transPrices[i].id) {
						optStr = optStr + "<option selected value=" + transPrices[i].id + ">" + transPrices[i].company.companyName + " " + transPrices[i].privateBaseWeight + "KG " + transPrices[i].privateBasePrice + "元起价 " + transPrices[i].privateRenewalPrice + "元/KG续价" + "</option>";
					} else if(!isreadonly) {
						optStr = optStr + "<option value=" + transPrices[i].id + ">" + transPrices[i].company.companyName + " " + transPrices[i].privateBaseWeight + "KG " + transPrices[i].privateBasePrice + "元起价 " + transPrices[i].privateRenewalPrice + "元/KG续价" + "</option>";
					}
				}
				$(companyElement).append(optStr);
				if(callback) callback();
			}
		}
	});
}